package com.fearless.crm.workbench.service.impl;

import com.fearless.crm.settings.dao.UserDao;
import com.fearless.crm.settings.entity.User;
import com.fearless.crm.vo.PaginationVo;
import com.fearless.crm.workbench.dao.ActivityDao;
import com.fearless.crm.workbench.dao.ActivityRemarkDao;
import com.fearless.crm.workbench.entity.Activity;
import com.fearless.crm.workbench.entity.ActivityRemark;
import com.fearless.crm.workbench.service.ActivityService;
import com.fearless.crm.workbench.service.ClueRemarkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private ActivityDao activityDao;

    @Resource
    private ActivityRemarkDao activityRemarkDao;

    @Resource
    private UserDao userDao;




//获取用户列表业务层方法
    public List<User> getUserListService() {

        List userList=new ArrayList();

        userList= activityDao.getUserListDao();
        return userList;
    }

//新增市场活动，业务层方法
    public Map saveActivityService(Activity activity) {
        Map map=new HashMap();
        int count=activityDao.saveActivity(activity);
        if(count!=1){
            map.put("success",false);
        }else {
            map.put("success",true);
        }
        return map;
    }

//分页查询业务层方法
    public PaginationVo<Activity> pageListService(Map map) {
    //业务层处理流程
    // 1.通过dao取得total
    //2.通过dao取得datalist
    //3.将total和datalist封装到vo
    //将vo返回给处理器方法
        int total=activityDao.getTotalByCondition(map);
        List<Activity> datalist=activityDao.getActivityListByCondition(map);

      PaginationVo<Activity> paginationVo=new PaginationVo();
        paginationVo.setTotal(total);
        paginationVo.setDatalist(datalist);
        return paginationVo;
    }

//删除市场活动的业务层方法
    public boolean deleteService(String[] ids) {
        boolean flag=true;
        //查询出【需要】删除的备注的数量
        int count1=activityRemarkDao.getCountByAids(ids);

        //删除备注，返回一个受到影响的记录条数(实际删除数量)
        int count2=activityRemarkDao.deleteByAids(ids);

        if(count1!=count2){
            flag=false;
        }
        //删除市场活动
        int count3=activityDao.deleteByAid(ids);
        if(ids.length!=count3){
            flag=false;
        }
        return flag;
    }

//获取用户列表以及市场活动，业务层方法
    public Map getUserListAndActivity(String id) {
        Map map=new HashMap();
        //获取userList
        List<User> userList=userDao.getUserListDao();

        //获取activity
        Activity activity=activityDao.getById(id);

        //将userList和activity打包到map，然后返回
        map.put("userList",userList);
        map.put("activity",activity);

        return map;
    }

//修改市场活动操作
    public boolean updateService(Activity activity) {
        Boolean flag=true;
        int count=activityDao.updateDao(activity);
        if(count!=1){
            flag=false;
        }
        return flag;
    }


//查看详细信息页
    public Activity detailService(String id) {

       Activity activity=activityDao.detailDao(id);

        return activity;
    }


//查看备注信息页
    public List<ActivityRemark> getRemarkListByAid(String Aid) {
        List<ActivityRemark> remarkList=activityRemarkDao.getRemarkListByAidDao(Aid);
        return remarkList;
    }

//删除备注信息，业务层方法
    public boolean deleteRemarkByIdService(String id) {
        boolean flag=true;
        int count=activityRemarkDao.deleteByIdDao(id);
        if (count!=1){
            flag=false;
        }
        return flag;
    }

//新增备注信息，业务层方法
    public boolean addRemarkService(ActivityRemark activityRemark) {
        boolean flag=true;
        int count=activityRemarkDao.addRemarkDao(activityRemark);
        if(count!=1){
            flag=false;
        }
        return flag;
    }


//编辑备注信息，业务层方法，打开修改备注的模态窗口
    public Map<String,Object> editRemarkService(String id) {
        boolean flag=true;
        ActivityRemark activityRemark=activityRemarkDao.editRemarkDao(id);
        if(activityRemark==null){
            flag=false;
        }
        Map map=new HashMap();
        map.put("success",flag);
        map.put("activityRemark",activityRemark);

        return map;
    }


//编辑备注信息，业务层方法，执行修改备注的操作
    public boolean updateRemarkService(ActivityRemark activityRemark) {
        boolean flag=true;
        int count=activityRemarkDao.updateRemarkDao(activityRemark);
        if(count!=1){
            flag=false;
        }
        return flag;
    }

//在详细信息页中打开修改市场活动信息的模态窗口,业务层方法
    public Map<String, Object> getUserListAndActivityInDetailService(String id) {
        Map map=new HashMap();
       List<User> userList= userDao.getUserListDao();
       Activity activity=activityDao.getById(id);
       map.put("userlist",userList);
       map.put("activity",activity);

        return map;
    }



//根据名称模糊查询线索关联的市场活动，并且排除掉线索已经关联的市场活动
    public List<Activity> getActivityByNameAndNotConnectClueService(Map map) {
        List<Activity> activityList=activityDao.getActivityByNameAndNotConnectClueDao(map);


        return activityList;
    }



//根据名称模糊查询市场活动
    public List<Activity> queryActivityByNameService(String name) {
       List<Activity> activityList= activityDao.queryActivityByNameDao(name);
        return activityList;
    }




}
